/* 
Copyright (c) 2004,2005, 2006, 2007, 2008 University of Illinois 

All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:

1. Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.

3. The name of the author may not be used to endorse or promote products
   derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

/*
Functions common to single treebox dialogs
*/
AccessibilityExtension.ns(function() { with (AccessibilityExtension.LIB) {
	
	var AE = AccessibilityExtension.ns;  

	var RefreshList_custom = false;
	var post_InitAccessibilityExt = false;
	var updatePointers_custom = false;
	var activation_custom = false;
	
AccessibilityExtension.LIB.extend(AccessibilityExtension.LIB,
{
	InitAccessibilityExt_common: function (optionalIsSidebar) {
		AE.sortColumn = "list-header-order";
		AE.sortDirectionAscending = true;//use a toggle, so we can just negate
		AE.sortDirection = AE.sortDirectionAscending;
		
		if (optionalIsSidebar === undefined) {
	    	window.focus(); //SMF 6/21/07
		} 
		
		updatePointers();
		if (AE.treebox == null) {
			alert(accessext_getBundleString('varisnull',"Treebox"));
		}
		if(AE.mydoc == null) {
			alert(accessext_getBundleString('varisnull',"mydoc"));
		}
		if(AE.treebox != null && AE.mydoc != null) {
			RefreshList();
			accessext_loadDialogPrefs();
			accessext_setNoteCaption();
			document.addEventListener("message", receiveMessage, false);
			if (post_InitAccessibilityExt) {
				post_InitAccessibilityExt();
			}
			return true;
		} else {
			return false;
		}
	},
	
	InitAccessibilityExt: function () {
		return InitAccessibilityExt_common();
	},
	
	UpdateTreeView: function() {
		var mapping = new Object();//loading unncessary, all cols use the format: cols['list-header-order'] = 'order';
		UpdateTreeView_common(AE.treebox,'order',AE.sortColumn,mapping,AE.dataarray);	
		return;
	},
	
	RefreshList: function() {
		if (RefreshList_custom) {
			RefreshList_custom();
		} else {
			updatePointers();
			AE.dataarray = new Array();
			
			var documents = accessext_getDocuments(AE.thewindow,new Array());
			
			for (var i =0; i < documents.length; i++) {
				ParseDocument(documents[i]);
			}
			UpdateTreeView();
		}
	},
	
	/*
	Dialog box destructor
	*/
	CleanUp: function() {
		return accessext_GlobalCleanup();
	},
	
	updatePointers: function() {
		if (updatePointers_custom) {
			updatePointers_custom();
		} else {
			try { // from the accessibility Issues Dialog
				if (window.opener.name == 'accessext-issues') {
					var accessibilityIssuesDialog = window.opener.parent;
					AE.thewindow = accessibilityIssuesDialog.opener.parent._content;			
				} else {
					AE.thewindow = window.opener.parent._content;
				}		
			} catch(e) { 
				try {// dialog way first
					AE.thewindow = window.opener.parent._content;
				}
				catch(e) { 
					try {//sidebar way second
						AE.thewindow = window.parent._content;
					}catch(e) {
						accessext_consoleDump('updatePointers exception: ', ex);
					}
				}
			}
			AE.mydoc = AE.thewindow.document;
			AE.treebox = document.getElementById("treebox");
			AE.dataarray = new Array();
		}
	},
	
	Activate: function() {
		CleanUp();
		if (activation_custom) {
			var theIndex = AE.treebox.currentIndex;
			if (theIndex < 0 || theIndex >= AE.dataarray.length  || theIndex == null) {
				return false;
			}
			return activation_custom(theIndex);
		}
	},
	
	accessext_handle_treebox: function() {
		if (AE.treebox.currentIndex < 0 || AE.treebox.currentIndex >= AE.dataarray.length) {
			return;
		}
		
		var findex = AE.treebox.currentIndex;
		var node = AE.dataarray[findex].node;
		if(node == null || AE.mydoc == null) {
			return;
		}
	
		accessext_treeboxScrollAndHighlightNode(); 
		if (AE.treebox.currentIndex < 0 || AE.treebox.currentIndex >= AE.dataarray.length) {
			return;
		}
		accessext_setNoteText(findex);	
	},
	
	accessext_setNoteText: function(arrayindex) {
	 	var noteElement = document.getElementById("label-note-text");
		if (null!= noteElement) {
	        noteElement.setAttribute("value",getNoteText(AE.dataarray[arrayindex]));
		}	
	}
});
}});	